package com.macro.mall.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.GoodsManagementDto;
import com.macro.mall.dto.GoodsTypeDao;
import com.macro.mall.service.GoodsManagementService;
import com.macro.mall.util.OssUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@Tag(name = "GoodsManagementController", description = "物品管理")
@RequestMapping("/goodsManagement")
public class GoodsManagementController {
    @Autowired
    private GoodsManagementService goodsManagementService;





    /**
     *  查询物品管理列表
     * @return
     */
    @Operation(summary = "获取物品管理列表")
    @RequestMapping(value = "/goodsManagementList", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<GoodsManagementDto>> getGoodsManagementList(){
        List<GoodsManagementDto> list= goodsManagementService.getGoodsManagementList();
        return CommonResult.success(list);
    }

    /**
     *  获取物品类型
     * @return
     */
    @Operation(summary = "获取物品类型")
    @RequestMapping(value = "/goodsType", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<GoodsTypeDao>> getGoodsType(){
       List <GoodsTypeDao >goodsType= goodsManagementService.getGoodsType();
        return CommonResult.success(goodsType);
    }
    /**
     *  新增物品管理
     * @param goodsManagement
     * @return
     */
   @Operation(summary = "新增物品管理")
   @RequestMapping(value = "/goodsManagementAdd", method = RequestMethod.POST)
   @ResponseBody
   public CommonResult getGoodsManagementAdd(@RequestBody GoodsManagementDto goodsManagement){
       final int i = goodsManagementService.goodsManagementAdd(goodsManagement);
       if (i > 0){
           return CommonResult.success(i);
       }
     return CommonResult.failed();
   }

    /**
     *  修改物品管理
     * @param goodsManagement
     * @return
     */
    @Operation(summary = "修改物品管理")
    @RequestMapping(value = "/goodsManagementUpdate", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult goodsManagementUpdate(@RequestBody GoodsManagementDto goodsManagement){
        int count=  goodsManagementService.goodsManagementUpdate(goodsManagement);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     *  删除物品管理
     * @param id
     * @return
     */
    @Operation(summary = "删除物品管理")
    @RequestMapping(value = "/goodsManagementDelete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult goodsManagementDelete(@PathVariable Integer id){
        int count= goodsManagementService.goodsManagementDelete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     *  根据id查询物品管理对象
     * @param id
     * @return
     */
    @Operation(summary = "查询物品管理对象")
    @RequestMapping(value = "/goodsManagementSearch/{id}", method = RequestMethod.GET)
    @ResponseBody
    public  CommonResult goodsManagementSearch(@PathVariable Integer id){
        GoodsManagementDto goodsManagement= goodsManagementService.goodsManagementSearch(id);
        return CommonResult.success(goodsManagement);
    }



    @Operation(summary = "上传图片")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult upload(@RequestPart("files") MultipartFile files) {
        final String s = OssUtil.uploadMultipartFile(files);
        return CommonResult.success(s);
    }




}
